package com.zeny.xdclass.utils;

import java.security.MessageDigest;
import java.util.UUID;

/**
 * @ClassName CommonUtils
 * @Description 常用工具类, MD5, UUID, 雪花算法分布式ID
 * @Author zeny
 */
public class CommonUtils {
    private static final SnowFlake SNOW_FLAKE = new SnowFlake(5, 5);

    /**
     * @Description 生成UUID
     * @param  
     * @return java.lang.String
     **/
    public static String generateUUID() {
        String uuid = UUID.randomUUID().toString()
                .replace("-", "")
                .substring(0, 32);
        return uuid;
    }
    
    /**
     * @Description  md5工具类
     * @param data 
     * @return java.lang.String
     **/
    public static String MD5(String data) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] arr = md5.digest(data.getBytes("UTF-8"));
            StringBuilder sb = new StringBuilder();
            for (byte item : arr) {
                sb.append(Integer.toHexString((item & 0xFF | 0x100)).substring(1, 3));
            }
            return sb.toString();
        }catch (Exception e) {
            e.printStackTrace();   
        }
        return null;
    }

    /**
     * @Description  雪花算法-分布式ID
     * @param
     * @return java.lang.String
     **/
    public static String snowIdStr() {
        return SNOW_FLAKE.nextId() + "";
    }

    /**
     * @Description  雪花算法-分布式ID
     * @param
     * @return java.lang.String
     **/
    public static Long snowIdLong() {
        return SNOW_FLAKE.nextId();
    }


    public static void main(String[] args) {
        for (int i = 0; i < 1000000000; i++) {
            long l = SNOW_FLAKE.nextId();
            System.out.println(l);
        }
    }
}
